//
//  newFeaturesViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/25.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "newFeaturesViewController.h"
#import "NewPicViewController.h"
//偏好设置
#define DWUser_Defaults [NSUserDefaults standardUserDefaults]

@interface newFeaturesViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, weak)UIImageView *lastImgView;
@property (nonatomic, strong) NSArray *arrayPic;
@property (nonatomic, strong) UIButton *btn;

@end

@implementation newFeaturesViewController
#define key_ShortVersion @"key_ShortVersion"
#pragma mark ---Appdelegate设置引导页控制器
+ (void)dw_AppdelegateNewFeaturesWindow:(UIWindow *)window newFeaturesVC:(id)newFeaturesVC mainVC:(id)mainVC {
    NSString *key = @"CFBundleShortVersionString";
    
    // 获得当前软件的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    // 获得沙盒中存储的版本号
    NSString *sanboxVersion = [[NSUserDefaults standardUserDefaults] stringForKey:key];
    
    //判断当前本地保存的版本号为空,或者本地保存的版本号小于当前app的版本号,就直接进入到新特性页面
    if (![currentVersion isEqualToString:sanboxVersion]) {
        
        //进入新特性控制器之前,保存一下当前app的版本号,以便下次进入的时候判断
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        //加载新特性页面
        window.rootViewController = newFeaturesVC;
        
    }else{
        
        //加载首页控制器
        window.rootViewController = mainVC;
        
    }
    
    [window makeKeyAndVisible];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    int count = 4;
    self.scrollView.frame = CGRectMake(0, 0, GYMScreenW, GYMScreenH);
    CGFloat w = self.scrollView.frame.size.width;
    CGFloat h = self.scrollView.frame.size.height;
    
    for (int i = 0; i<count; i++) {
        // 创建
        UIImageView *imageView = [[UIImageView alloc] init];
        NSString *name = [NSString stringWithFormat:@"NewFeatures%d", i];
        imageView.image = [UIImage imageNamed:name];
        // frame
        imageView.frame = CGRectMake(i * w, 0, w, h);
        
        [self.scrollView addSubview:imageView];
        if (i == count - 1) {
            self.lastImgView = imageView;
        }
        
        if (i == 3) {
            imageView.userInteractionEnabled = YES;
            
            
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,GYMScreenH / 4 * 3, GYMScreenW, GYMScreenH)];
            
//            [button setTitle:@"进入主页" forState:UIControlStateNormal];
            
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            button.backgroundColor = [UIColor clearColor];
            
            [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
            
            [imageView addSubview:button];
            
            self.btn = button;
        }
    }
    
    self.arrayPic = @[@"notice4",@"notice3",@"notice2",@"notice1"];
    
    // 设置内容大小
    CGFloat contentW = count * w;
    self.scrollView.contentSize = CGSizeMake(contentW, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    // 开启分页功能
    self.scrollView.pagingEnabled = YES;
    // 总页数
//    self.pageControl.numberOfPages = count;
    
    
    
   
}
- (void)click {
    NewPicViewController *vc = [[NewPicViewController alloc]init];
    vc.arrImg = self.arrayPic;
    [self showViewController:vc sender:self];
    
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    self.view.frame = CGRectMake(0, 0, GYMScreenW, GYMScreenH);
}

#pragma mark - <UIScrollViewDelegate>
/**
 * 只要scrollView在滚动，就会调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 0.3 > (int)(0.3 + 0.5) > 0
    // 0.6 > (int)(0.6 + 0.5) > 1
    // 小数四舍五入为整数 ： (int)(小数 + 0.5)
//    int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
//    self.pageControl.currentPage = page;
    
    
}

- (NSArray *)arrayPic{
    if (!_arrayPic) {
        _arrayPic = [NSArray array];
    }
    return _arrayPic;
}


@end
